package com.antdesignbackend.backend.service.impl;

import com.antdesignbackend.backend.entity.Button;
import com.antdesignbackend.backend.entity.RoleButton;
import com.antdesignbackend.backend.mapper.RoleButtonMapper;
import com.antdesignbackend.backend.service.ButtonService;
import com.antdesignbackend.backend.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    RoleButtonMapper roleButtonMapper;
    @Autowired
    ButtonService buttonService;

    @Override
    public List<Button> filterPermission(String roleIDs, int menuID) {
        if (roleIDs != null && !roleIDs.equals("") && menuID > 0) {
            //先查询当前登录角色具备哪些button功能
            QueryWrapper<RoleButton> roleButtonQueryWrapper = new QueryWrapper<>();
            roleButtonQueryWrapper.in("roleID", roleIDs);
            List<RoleButton> list = roleButtonMapper.selectList(roleButtonQueryWrapper);
            //再根据得到的功能id，查询对应当前菜单页面下，具备哪些功能
            QueryWrapper<Button> buttonQueryWrapper = new QueryWrapper<>();
            buttonQueryWrapper.eq("menuID", menuID).eq("activity", 1);
            List<Button> list1 = buttonService.list(buttonQueryWrapper);

            List<Button> newList = new ArrayList<>();
            list1.forEach(entity -> list.stream().
                    filter(roleButton -> entity.getId() == roleButton.getButtonID()).map(roleButton -> entity).forEachOrdered(newList::add));
            return newList;
        } else {
            return null;
        }
    }
}
